package com.subframe7536.UserInfo.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subframe7536.UserInfo.entity.User;
import com.subframe7536.UserInfo.mapper.UserMapper;
import com.subframe7536.UserInfo.utils.JwtUtil;
import com.subframe7536.UserInfo.utils.RedisCacheUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.kafka.core.KafkaTemplate;

import javax.annotation.Resource;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author subframe7536
 */
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    public UserMapper userMapper;

    @Resource
    public RedisCacheUtil cache;

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @Override
    @DS("slave")
    public org.example.service.VO.User getInfo(String token) {
        String phone = JwtUtil.parseJWT(token).getSubject();
        String userId = userMapper.getUserIdByPhone(phone);
        User one = new LambdaQueryChainWrapper<>(userMapper).eq(User::getUserId, userId).one();
        //向kafka发送消息
//        try {
//            kafkaTemplate.send("login", "test", one.getNickName() + "login");
//        } catch (Exception ignored) {
//        }
        org.example.service.VO.User target = new org.example.service.VO.User();
        BeanUtils.copyProperties(one, target);
        return target;
    }

}
